package prueba.principal.beans;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import prueba.principal.actions.UsersAction;

public class UserBean {
	
	private int id;
	private String user;
	private String password;	
	private boolean error=false;	
	private HttpSession session;
	
	public UserBean() {
		FacesContext context= FacesContext.getCurrentInstance();
		session =(HttpSession)context.getExternalContext().getSession(true); 
		error=false;
	}

	public boolean isError() {
		return error;
	}


	public void setError(boolean error) {
		this.error = error;
	}



	public int getId() {
		return id;
	}



	public void setId(int id) {
		this.id = id;
	}



	public String getUser() {
		return user;
	}



	public void setUser(String user) {
		this.user = user;
	}



	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}



	public String login(){
		
		UsersAction login = new UsersAction();
		session.removeAttribute("username");
		
		if(login.compruebaUserPwd(user, password)){
			this.error=false;						
						
			session.setAttribute("username", user); 			
			
			return "OK";
		}else{
			this.error=true;
			return "KO";
		}
		
	}
	 
	public String logout(){
		
		session.removeAttribute("username");
		
		return "OK";
	}
	
	public String volver(){
				
		
		return "volver";
	}
	
	public String listUsers(){
		
		FacesContext context= FacesContext.getCurrentInstance();		
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
		
		UsersAction login = new UsersAction();
		
		request.setAttribute("lista",login.listaUsuarios());
		
		return "list";
		
	}
}
